package com.warren.test;

/**
 * @version V1.0
 * @Description: 懒汉式模式
 * @author: warren
 * @date: 2022/7/8$ 16:20$
 */
public class Singleton {
    //线程安全的单列模式
    private volatile static Singleton uniqueInstance;
    private Singleton() {

    }
    public static Singleton getUniqueInstance() {
        if (uniqueInstance == null) {
            synchronized (Singleton.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new Singleton();
                }
            }
        }
        return uniqueInstance;
    }
//    private static Singleton instance = null;
//
//    private void Singleton() {
//    }
//
//    public static Singleton getInstance() {
//        if (instance == null) {
//            synchronized (Singleton.class) {
//                if (instance == null) {
//                    instance = new Singleton();
//                }
//            }
//        }
//        return instance;
//    }
}


//饿汉式
//public class Singleton {
//    // 服务器启动时创建
//    private static Singleton instance = new Singleton();
//
//    private Singleton() {
//    }
//
//    public static Singleton getInstance() {
//        return instance;
//    }
//}
